package com.reader.api.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里短信配置.
 *
 * @author liujianqing
 * @date 2018/10/24
 */
@Configuration
public class AliSmsConfig {
    private static final Logger logger = LoggerFactory.getLogger(AliSmsConfig.class);

    @Value("${ali.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${ali.sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${ali.sms.product}")
    private String product;
    @Value("${ali.sms.domain}")
    private String domain;
    @Value("${ali.sms.endpointName}")
    private String endpointName;
    @Value("${ali.sms.regionId}")
    private String regionId;

    @Bean(name = "acsClient")
    public IAcsClient acsClient() {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(endpointName, regionId, product, domain);
        } catch (ClientException e) {
            logger.error("intial sms client error:"+e);
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        return acsClient;
    }
}
